﻿/// <reference path="Repeater.js" />

var ____a;

var $RepeaterEntriesEventArgs = TA.Turbo.Controls.RepeaterEntriesEventArgs = function RepeaterEntriesEventArgs(firstIndex, numEntries)
{
	this.FirstIndex = firstIndex;
	this.NumEntries = numEntries;
}
$RepeaterEntriesEventArgs.Inherit(BaseJsonSerializable);

$RepeaterEntriesEventArgs.prototype.FirstIndex = 0;
$RepeaterEntriesEventArgs.prototype.NumEntries = 0;

$RepeaterEntriesEventArgs.prototype.SerializeContents = function SerializeContents(jw)
{
	jw.Property("F", this.FirstIndex);
	jw.Property("N", this.NumEntries);
}



$Repeater.prototype.UpdateDisplay = function UpdateDisplay()
{
	var displayStart = 0, displayEnd = this.get_Entries().length;

	if (this.get_Pager().Size != 0)
	{
		displayStart = this.get_ActualPageIndex() * this.get_Pager().Size;
		displayEnd = Math.min(displayStart + this.get_Pager().Size, this.get_Entries().length);
	}

	if (this._AllEntriesLoaded(displayStart, displayEnd))
	{
		this._DisplayItems(displayStart, displayEnd);
	}
	else
	{
		var unloadedStart = displayStart, unloadedEnd = displayEnd;
		for (; unloadedStart < displayEnd && this.get_Entries()[unloadedStart] != $Repeater.NotLoaded; ++unloadedStart) { }
		for (; unloadedEnd >= displayStart && this.get_Entries()[unloadedEnd - 1] != $Repeater.NotLoaded; --unloadedEnd) { }

		this._LoadEntries(unloadedStart, unloadedEnd);
	}
}
$Repeater.prototype.UpdatePages = function UpdatePages(statusOnly)
{
	var pagesRangeModified = true;
	if (this._oldPagesRange != null)
	{
		if (this._oldPagesRange.Start == this.get_PagesRange().Start && this._oldPagesRange.End == this.get_PagesRange().End)
			pagesRangeModified = false;
	}
	if (!statusOnly || pagesRangeModified)
	{
		var minimumPages = this.get_Pager().DisplayMode; // cheat - should be a switch statement because DisplayMode is an "enum"

		var displayPager = (this.get_Pager().Size != 0) && this.get_NumPages() >= minimumPages;

		this._pagerTop.set_Visible(displayPager && (this.get_Pager().Position & $PagerPosition.Top) == $PagerPosition.Top);
		this._pagerBottom.set_Visible(displayPager && (this.get_Pager().Position & $PagerPosition.Bottom) == $PagerPosition.Bottom);

		if (displayPager)
		{
			this._pagerTop.UpdateStyle();
			this._pagerBottom.UpdateStyle();
		}
	}
	else
	{
		this._pagerTop.UpdateStatus();
		this._pagerBottom.UpdateStatus();
	}
}
$Repeater.prototype._DisplayItems = function _DisplayItems(displayStart, displayEnd)
{
	this._DisplayItems_Reset();
	this._DisplayItems_Perform(displayStart, displayEnd);
	this._DisplayItems_CleanUp();
}
$Repeater.prototype._DisplayItems_Reset = function _DisplayItems_Reset()
{
	this.get_UnusedItems().AddRange(this.get_Items());
	this.get_Items().Clear();
}
$Repeater.prototype._DisplayItems_Perform = function _DisplayItems_Perform(displayStart, displayEnd)
{
	for (var iEntry = displayStart; iEntry < displayEnd; ++iEntry)
	{
		var item = this._LeaseItem(this.get_DefaultItemType(), null);
		this._SetItemValue(item, iEntry);
	}
}
$Repeater.prototype._DisplayItems_CleanUp = function _DisplayItems_CleanUp()
{
	while (this.get_UnusedItems().length > 0)
	{
		var item = this.get_UnusedItems().pop();
		item.Hide();
	}
}
$Repeater.prototype._LoadEntries = function _LoadEntries(start, end)
{
	var obj = this;
	var args = new $RepeaterEntriesEventArgs(start, end - start);
	this.RaiseServerEvent("LoadEntries", args, obj._LoadEntries_CallBack.bind(this));
}
$Repeater.prototype._LoadEntries_CallBack = function _LoadEntries_CallBack(response)
{
	var firstIndex = response.Main["F"],
		entriesJson = response.Main["E"];

	for (var iEntry = 0; iEntry < entriesJson.length; ++iEntry)
	{
		this.get_Entries()[iEntry + firstIndex] = this.get_Value().DeserializeOneEntry(entriesJson[iEntry]);
	}

	this.UpdateDisplay();
	this.UpdatePages();
}
$Repeater.prototype._LeaseItem = function _LeaseItem(type, refItem)
{
	var fn = function(k) { return k.get_Type() == type; };

	var item = this.get_UnusedItems().First(fn);
	if (____a) item = new $RepeaterItem();

	if (item != null)
	{
		this.get_UnusedItems().Remove(item);
		if (!item.get_Visible())
		{
			item.Show(this.get_ContainerElement(), refItem);
		}
	}
	else
	{
		item = this._CreateNewItem(type);
		item.Show(this.get_ContainerElement(), refItem);
	}

	this.get_Items().push(item);

	return item;
}
$Repeater.prototype._CreateNewItem = function _CreateNewItem(type)
{
	var item = new $RepeaterItem(this, type, this.get_AllItems().length.toString());
	this.get_AllItems().push(item);
	return item;
}
$Repeater.prototype._ReleaseItem = function _ReleaseItem(item)
{
	this.get_Items().Remove(item);
	this.get_UnusedItems().push(item);

	item.Hide();
}
$Repeater.prototype._GetItemValue = function _GetItemValue(item)
{
	var args = { Item: item, Value: new Object() };

	item.Store(args.Value);

	this.RaiseEvent("GetItemValue", args);

	return args.Value;
}
$Repeater.prototype._SetItemValue = function _SetItemValue(item, valueIndex)
{
	var args = {
		Container: item,
		DataEntry: valueIndex == null ? null : this.get_Entries()[valueIndex]
	};

	item.Bind(args.DataEntry);

	this.RaiseEvent("BindItem", args);
	item.set_ValueIndex(valueIndex);
}
$Repeater.prototype._AllEntriesLoaded = function _AllEntriesLoaded(start, end)
{
	for (var iEntry = start; iEntry < end; ++iEntry)
	{
		if (this.get_Entries()[iEntry] == $Repeater.NotLoaded)
			return false;
	}
	return true;
}
